<template>
  <view class="lawyer-register">
    <u-form :model="formData" ref="uForm">
      <u-form-item label="姓名" prop="name">
        <u-input v-model="formData.name" placeholder="请输入真实姓名" />
      </u-form-item>
      
      <u-form-item label="执业证号" prop="licenseNumber">
        <u-input v-model="formData.licenseNumber" placeholder="请输入律师执业证号" />
      </u-form-item>
      
      <u-form-item label="执业年限" prop="experience">
        <u-input v-model="formData.experience" type="number" placeholder="请输入执业年限" />
      </u-form-item>
      
      <u-form-item label="专业领域" prop="expertise">
        <u-checkbox-group v-model="formData.expertise">
          <u-checkbox v-for="(item, index) in expertiseOptions" 
            :key="index" 
            :label="item.label" 
            :name="item.value">
          </u-checkbox>
        </u-checkbox-group>
      </u-form-item>
      
      <u-form-item label="执业证书照片" prop="licensePic">
        <u-upload
          :fileList="fileList"
          @afterRead="afterRead"
          @delete="deletePic"
          :maxCount="1"
        />
      </u-form-item>
      
      <u-form-item label="个人简介" prop="introduction">
        <u-textarea v-model="formData.introduction" placeholder="请输入个人简介" />
      </u-form-item>
    </u-form>
    
    <view class="submit-btn">
      <u-button type="primary" @click="submitForm">提交申请</u-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        name: '',
        licenseNumber: '',
        experience: '',
        expertise: [],
        licensePic: '',
        introduction: ''
      },
      expertiseOptions: [
        { label: '婚姻家事', value: 'FAMILY' },
        { label: '刑事辩护', value: 'CRIMINAL' },
        { label: '合同纠纷', value: 'CONTRACT' },
        { label: '知识产权', value: 'IP' },
        { label: '劳动争议', value: 'LABOR' }
      ],
      fileList: [],
      rules: {
        name: [{
          required: true,
          message: '请输入姓名',
          trigger: ['blur', 'change']
        }],
        licenseNumber: [{
          required: true,
          message: '请输入执业证号',
          trigger: ['blur', 'change']
        }],
        experience: [{
          required: true,
          message: '请输入执业年限',
          trigger: ['blur', 'change']
        }],
        expertise: [{
          required: true,
          message: '请选择专业领域',
          trigger: ['change']
        }],
        licensePic: [{
          required: true,
          message: '请上传执业证书照片',
          trigger: ['change']
        }]
      }
    }
  },
  methods: {
    // 上传图片后的回调
    async afterRead(event) {
      const { file } = event.detail;
      try {
        const formData = new FormData();
        formData.append('file', file);
        
        const response = await uni.uploadFile({
          url: '/api/lawyer/upload',
          filePath: file.url,
          name: 'file',
          header: {
            'Content-Type': 'multipart/form-data'
          }
        });
        
        if (response.statusCode === 200) {
          const data = JSON.parse(response.data);
          this.formData.licensePic = data.fileUrl;
          this.fileList.push({
            url: data.fileUrl,
            status: 'success',
            message: '上传成功'
          });
        }
      } catch (error) {
        uni.showToast({
          title: '上传失败',
          icon: 'none'
        });
      }
    },
    
    // 删除图片
    deletePic(event) {
      const index = event.index;
      this.fileList.splice(index, 1);
      this.formData.licensePic = '';
    },
    
    // 提交表单
    async submitForm() {
      try {
        const valid = await this.$refs.uForm.validate();
        
        if (valid) {
          const response = await uni.request({
            url: '/api/lawyer/register',
            method: 'POST',
            data: this.formData,
            header: {
              'Content-Type': 'application/json'
            }
          });
          
          if (response.statusCode === 200) {
            uni.showToast({
              title: '提交成功',
              icon: 'success'
            });
            // 跳转到审核中页面
            setTimeout(() => {
              uni.navigateTo({
                url: '/pages/mine/register-status'
              });
            }, 1500);
          }
        }
      } catch (error) {
        uni.showToast({
          title: '提交失败',
          icon: 'none'
        });
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.lawyer-register {
  padding: 20rpx;
  
  .submit-btn {
    margin-top: 40rpx;
    padding: 0 20rpx;
  }
}
</style> 